import http from "http";
import { utils } from "../../util/utils";


export default class LHttp {
    static syncget(url: string, callback: (res: any) => void) {
        http.get(url, (res) => {
            var req = ""
            res.on("data", (data) => {
                req += data
            })
            res.on("end", () => {
                if (req == "ok") {
                    req = utils.safeJson(req);
                    if (callback) {
                        callback(req);
                    }
                }
            })
        }).on("error", (e) => {
            if (callback) {
                callback("error");
            }
        });
    }

    static async get(url: string) {
        return new Promise<any>((resolve, reject) => {
            http.get(url, (res) => {
                var req = ""
                res.on("data", (data) => {
                    req += data
                })
                res.on("end", () => {
                    req = utils.safeJson(req);
                    resolve(req);
                })
            }).on("error", (e) => {
                resolve("error");
            });
        });
    }
}
